package com.hlx.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hlx.entity.user.Customer;
import com.hlx.service.user.CustomerService;
import lombok.extern.slf4j.Slf4j;
import com.hlx.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @PostMapping("/getCustomer")
    public Result<List<Customer>> getAllCustomer(@RequestBody Customer cu) {
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Customer::getCustomerUser,cu.getCustomerUser());
        List<Customer> list = customerService.list(queryWrapper);
        if(list.isEmpty()){
            return Result.error("无常用乘车人信息！");
        }
        return Result.success(list);
    }

    @PostMapping("/addCustomer")
    public Result<Customer> addCustomer(@RequestBody Customer cu) {
        boolean res = customerService.save(cu);
        if(!res){
            return Result.error("添加失败！");
        }
        return Result.success(cu);
    }

    @PutMapping("/updateCustomer")
    public Result<String> updateCustomer(@RequestBody Customer cu) {
        log.info("cu{}",cu);
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Customer::getCustomerIdentity,cu.getCustomerIdentity());
        Integer id = customerService.getOne(queryWrapper).getId();
        cu.setId(id);
        System.out.println(cu);
        boolean res = customerService.updateById(cu);
        if(!res){
            return Result.error("更新失败！");
        }
        return Result.success("更新成功！");
    }

    @DeleteMapping("/deleteCustomer")
    public Result<String> deleteCustomer(String id){
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Customer::getCustomerIdentity, id);
        boolean res = customerService.remove(queryWrapper);
        if(!res){
            return Result.error("删除失败！");
        }
        return Result.success("删除成功！");
    }

    @PostMapping("/deleteBulkCustomer")
    public Result<String> deleteBulkCustomer(@RequestBody List<Integer> ids){
        log.info("{}",ids);
        LambdaQueryWrapper<Customer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Customer::getId,ids);
        boolean res = customerService.remove(queryWrapper);
        if(!res){
            return Result.error("批量删除失败！");
        }
        return Result.success("批量删除成功");
    }
}
